package com.example.e_comm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.mapper.PermissionMapper;
import com.example.e_comm.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class PermissionServiceImpl
        extends ServiceImpl<PermissionMapper, Permission>
        implements PermissionService {
    @Autowired
    private PermissionMapper mapper;

    @Override
    public List<Permission> getPermissionsByPage(Page<Permission> page) {
        return mapper.selectList( page, null);
    }

    @Override
    public List<Permission> getAllPermissionsByPage(Page<Permission> page) {
        return null;
    }

    @Override
    public IPage<Permission> getPermissionsByPageWithCondition(Page<Permission> page, String keyword) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)){
            wrapper.like("perm_name",keyword)
                    .or().like("perm_key",keyword);
        }
        return mapper.selectPage(page,wrapper);
    }
    @Override
    public List<Permission> getPermissionsByUserId(Long userId) {
//        //实际项目中,判断逻辑会包含判断用户是否被禁用、角色是否被禁用、权限是否被禁用

        if (userId == null){
            return null;
        }
        if (userId == 0){
            return null;
        }
        else{
            if (userId == 1){
                return mapper.selectList(null);
            }

        }

        return mapper.getPermissionsByUserId(userId);
    }

    @Override
    public List<Permission> getPermissionsByRoleId(Long roleId) {

        return mapper.getPermissionsByRoleId(roleId);

    }

    @Override
    public Set<String> getPermissionCodesByUserId(Long userId) {
        List<Permission> list = getPermissionsByUserId(userId);
        return list.stream() //将list转换为流
                .map(Permission::getPermKey) //获取权限key
                .collect(Collectors.toSet()); //转换成Set
    }

    @Override
    public Set<String> getPermissionCodesByRoleId(Long roleId) {
        List<Permission> permissions = getPermissionsByRoleId(roleId);
        return permissions.stream()
                .map(Permission::getPermKey)
                .collect(Collectors.toSet());
    }

    @Override
    public Set<String> getPermissionsByUserIdSet(Set<Long> userId) {
        return null;
    }

    @Override
    public Set<String> getPermissionsByRoleIdSet(Set<Long> roleId) {
        return null;
    }
}
